<?php

/**
 * @author 	Samuel Carlier
 * @package P3Base_Mailer
 */
class P3Base_Mailer_Addresses {

	/**
	 * @var array
	 */
	private $_addresses = array();

	/**
	 * @param string $type to, cc, bcc and from
	 * @param string $name
	 * @param string $email
	 */
	public function add($type, $name, $email) {
		$this->_addresses[$type][] = array('name' => $name, 'email' => $email);
	}

	/**
	 * @param string $type to, cc, bcc and from
	 */
	public function clear($type) {
		if(is_null($type)) {
			$this->_addresses = array();	
		}

		unset($this->_addresses[$type]);
	}

	/**
	 * @param  string $type to, cc, bcc and from
	 * @return array
	 */
	public function get($type) {
		if(!in_array($type, array('to','cc','bcc','from','return_path', 'replay_to', 'receipt_to'))) {
			throw new P3Base_Mailer_Exception('invalid type "'.$type.'"');
		}

		if(!key_exists($type, $this->_addresses)) {
			return array();	
		}

		return $this->_addresses[$type];
	}

	/**
	 * @param string $type 			to, cc, bcc and from
	 * @param string $iteratorName 	mail
	 *
	 * @return Iterator
	 */
	public function getIterator($type, $iteratorName='mail') {
		$iteratorName = strtolower($iteratorName);

		switch($iteratorName) {
			case 'mail':
				$iterator = new P3Base_Mailer_Iterator_MailAddress($this->get($type));
			break;	
			
			default:
				throw new P3Base_Mailer_Exception('unsupported iterator: "'.$iteratorName.'"');
		}
		
		return $iterator;
	}
}

?>